package com.princess.demok.mvvm.view

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.princess.baselib.base.HintDialog
import com.princess.baselib.utils.LogUtils
import com.princess.baselib.utils.SharedPreferencesUtils
import com.princess.demok.R
import com.princess.demok.base.view.BaseLifeCycleActivity
import com.princess.demok.common.State
import com.princess.demok.common.StateType
import com.princess.demok.common.MainModel
import com.princess.demok.mvvm.viewmodel.HomeViewModel
import com.princess.demok.ui.adapter.MainMvvmAdapter
import com.princess.demok.ui.demo.BesselActivity
import com.princess.demok.ui.demo.fish.FishActivity
import com.princess.demok.ui.home.*
import com.princess.demok.utils.MyUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_recycler_just.*

/**
 * @author: xha
 * @date: 2021/6/2 17:12
 * @Description:
 */
class HomeMvvmActivity : BaseLifeCycleActivity<HomeViewModel>() {

    private val mainMvvmAdapter by lazy {
        MainMvvmAdapter()
    }

    override fun initDataObserver() {
        mViewModel.mThemIntData.observe(this, Observer {
            it?.let {
                if (it == 0) {
                    tvThemeMain.text = "白天模式"
                } else {
                    tvThemeMain.text = "夜间模式"
                }
                themeInt = it
                recreate()
            }
        })
    }

    companion object {
        @JvmStatic
        fun start(context: Activity) {
            val starter = Intent(context, HomeMvvmActivity::class.java)
            context.startActivity(starter)

        }
    }

    override fun getLayoutId(): Int = R.layout.activity_mvvm_main

    override fun initData() {
        setLeftShow(false)
        setTitleText(R.string.app_name)
        mViewModel.loadState.postValue(State(StateType.SUCCESS))

        setAdapter()

        setPermission()
    }


    private fun setPermission() {
        val rxPermissions = RxPermissions(this)
            .request(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            .subscribe { aBoolean ->
                if (aBoolean) {
                    LogUtils.i("获取权限成功")
                } else {
                    showHintDialog()
                }
            }
    }

    private fun showHintDialog() {
        val hintDialog = HintDialog(this)
        hintDialog.show()
        hintDialog.setHintText("请在应用权限页面开启相关权限")
        hintDialog.setLeftButton("退出应用", View.OnClickListener {
            hintDialog.dismiss()
            finish()
        })
        hintDialog.setRightButton("前往设置", View.OnClickListener {
            hintDialog.dismiss()
            MyUtils.gotoAppDetailIntent(this, 101)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 101) {
            setPermission()
        }
    }

    private fun setAdapter() {

        mRecyclerView.layoutManager = GridLayoutManager(this, 2)

        mRecyclerView.adapter = mainMvvmAdapter

        val list: MutableList<MainModel> = arrayListOf()
        list.apply {
            if (themeInt == 0) {
                add(MainModel("夜间模式", "moshi"))
            } else {
                add(MainModel("白天模式", "moshi"))
            }
//            add(MainModel("apk 快传", WifiActivity::class.java.name))
            add(MainModel("历史上的今天", HistoryListActivity::class.java.name))
            add(MainModel("开心一下", SmileActivity::class.java.name))
            add(MainModel("四川油价", OilPriceActivity::class.java.name))
            add(MainModel("驾照考试", QstBankActivity::class.java.name))
            add(MainModel("比价计算器", ""))
            add(MainModel("跑马灯文字", MarqueeActivity::class.java.name))
            add(MainModel("鲤鱼动画", FishActivity::class.java.name))
            add(MainModel("贝塞尔曲线", BesselActivity::class.java.name))
        }

        mainMvvmAdapter.setList(list)

        mainMvvmAdapter.setOnItemClickListener { adapter, view, position ->
            val mainListModel = adapter.getItem(position) as MainModel
            if (mainListModel.className == "moshi") {
                if (themeInt == 0) {
                    themeInt = 1
                    SharedPreferencesUtils.putShared("theme", themeInt)
                    mainListModel.name = "白天模式"
                    mainMvvmAdapter.setData(0,mainListModel)
                } else {
                    themeInt = 0
                    SharedPreferencesUtils.putShared("theme", themeInt)
                    mainListModel.name = "夜间模式"
                    mainMvvmAdapter.setData(0,mainListModel)
                }
                recreate()
                return@setOnItemClickListener
            }
            if (mainListModel.className.isEmpty()) return@setOnItemClickListener

            val intent = Intent(this, Class.forName(mainListModel.className))
            startActivity(intent)
        }
    }

    override fun initOnClickerListener() {
        super.initOnClickerListener()

    }

    override fun onBackPressed() {
//        super.onBackPressed()
        moveTaskToBack(true)
    }
}